﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class LoginForm : DevComponents.DotNetBar.Office2007Form
    {
        public static string username="";
        public static string qx;
        public LoginForm()
        {
            InitializeComponent();
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is Button) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_yhm.Text == "admin" && txt_mm.Text == "manager")
                {
                    qx = "all";
                    username = "超级管理员";
                    MainForm main = new MainForm();
                    main.Show();
                    this.Hide();
                }
                else
                {
                    using (SqlConnection con = new SqlConnection(SQL.connectDB))
                    {
                        SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
                        DataTable dt_ry = new DataTable();
                        SqlCommand cmd = new SqlCommand("SELECT * FROM ZD_RY LEFT JOIN ZD_QX ON QXMC=MC WHERE UID=@uid AND PAW=@paw", con);//sql语句
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@uid", txt_yhm.Text.ToString()), new SqlParameter("@paw", txt_mm.Text.ToString()) };
                        cmd.Parameters.AddRange(paras);
                        da.SelectCommand = cmd;//设置为已实例化SqlDataAdapter的查询命令
                        da.Fill(dt_ry);

                        if (dt_ry.Rows.Count == 0)
                        {
                            lab_error.Visible = true;
                            timer1.Enabled = true;
                        }
                        else
                        {
                            username = dt_ry.Rows[0][2].ToString();
                            qx = dt_ry.Rows[0][4].ToString();
                            if (qx == "")
                                MessageBox.Show("您还没有任何权限!");
                            else
                            {
                                MainForm main = new MainForm();
                                main.Show();
                                this.Hide();
                            }
                        }
                    }
                }
            }

            catch (Exception ee)
            { MessageBox.Show(ee.ToString()); }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lab_error.Visible = false;
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}